﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Security.AccessControl;
using QL_DTTN.DAL;
namespace QL_DTTN.DTO
{
  public  class ConnectionSQL
    {
        private string _SeverName = "";
        private bool _Authentication = false;
        private string _UserName = "";
        private string _Password = "";
        private string _DatabaseName = "";
        public ConnectionSQL(string severName, string databaseName)
        {
            SeverName = severName;
            DatabaseName = databaseName;
            Authentication = true;
        }
        public ConnectionSQL(string severName, string databaseName, string userName, string password)
        {
            SeverName = severName;
            DatabaseName = databaseName;
            UserName = userName;
            Password = password;
        }
        public ConnectionSQL()
        { }
        public string ConnectionString()
        {
            SqlConnectionStringBuilder str = new SqlConnectionStringBuilder();
            str.DataSource = SeverName;
            str.InitialCatalog = DatabaseName;
            if (Authentication)
            {
                str.IntegratedSecurity = Authentication;
            }
            else
            {
                str.UserID = UserName;
                str.Password = Password;
            }
            return str.ConnectionString;
        }

        public string DatabaseName
        {
            get { return _DatabaseName; }
            set { _DatabaseName = value; }
        }

        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }

        public string UserName
        {
            get { return _UserName; }
            set { _UserName = value; }
        }

        public bool Authentication
        {
            get { return _Authentication; }
            set { _Authentication = value; }
        }

        public string SeverName
        {
            get { return _SeverName; }
            set { _SeverName = value; }
        }
        public static bool TestConnection()
        {
            try
            {
                System.IO.FileInfo fiRoot = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
                XMLObject xml = new XMLObject(new System.Xml.Serialization.XmlSerializer(typeof(ConnectionSQL)));
                ConnectionSQL conSQL = new ConnectionSQL();
                object a = null;
                xml.ReadXML(ref a, fiRoot.Directory.Root.ToString() + "\\QLDTTNConfig\\Config.xml");
                conSQL = (ConnectionSQL)a;
                conSQL.SeverName = Functions.Enrcode(conSQL.SeverName);
                conSQL.DatabaseName = Functions.Enrcode(conSQL.DatabaseName);
                conSQL.UserName = Functions.Enrcode(conSQL.UserName);
                conSQL.Password = Functions.Enrcode(conSQL.Password);

                SqlDataProvider sdp = new SqlDataProvider(conSQL.ConnectionString());
                if (sdp.OpenConnection())
                {
                    sdp.CloseConnection();
                    ClsFunctions.strConnecSQL = conSQL.ConnectionString();
                    return true;
                }
                else return false;
            }
            catch
            {
                return false;
            }

        }
        public static bool TestConnection(ConnectionSQL conSQL)
        {
            try
            {
                //System.Windows.Forms.MessageBox.Show(conSQL.ConnectionString());
                SqlDataProvider sdp = new SqlDataProvider(conSQL.ConnectionString());
                
                if (sdp.OpenConnection())
                {
                    sdp.CloseConnection();
                    return true;
                }
                else return false;
            }
            catch
            {
                return false;
            }
        }
        public static bool CreateDirectoryConnection(ConnectionSQL conSQL)
        {
            try
            {
                    System.IO.FileInfo fiRoot = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
                    #region permission directory
                    DirectorySecurity myDirectorySecurity = new DirectorySecurity();
                    myDirectorySecurity.AddAccessRule(new FileSystemAccessRule("Everyone",
                                                        FileSystemRights.FullControl, AccessControlType.Allow));
                    #endregion
                    System.IO.FileInfo fi = new FileInfo(fiRoot.Directory.Root.ToString() + "\\QLDTTNConfig\\");
                    if (!fi.Directory.Exists)
                        System.IO.Directory.CreateDirectory(fiRoot.Directory.Root.ToString() + "\\QLDTTNConfig", myDirectorySecurity);
                    SetDirectoryPermission(fiRoot.Directory.Root.ToString() + "\\QLDTTNConfig");
                    //luu file config
                    //System.Windows.Forms.MessageBox.Show(fiRoot.Directory.Root.ToString() + "\\QLTBConfig\\Config.xml");
                XMLObject xml=new XMLObject(new System.Xml.Serialization.XmlSerializer(typeof (ConnectionSQL)));
                return xml.WriteXML(conSQL, fiRoot.Directory.Root.ToString() + "\\QLDTTNConfig\\Config.xml");      
                
            }
            catch 
            {
                return false;
            }
           
        }
        private static void SetDirectoryPermission(string strDirectory)
        {
            DirectoryInfo myDirectoryInfo = new DirectoryInfo(strDirectory);
            DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
            myDirectorySecurity.AddAccessRule(new FileSystemAccessRule("Everyone",
                                              FileSystemRights.FullControl, AccessControlType.Allow));
            myDirectoryInfo.SetAccessControl(myDirectorySecurity);
        }
    }
}
